package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Business;
import com.travel.travelServer.entity.StoreCategory;
import com.travel.travelServer.mapper.BusinessMapper;
import com.travel.travelServer.mapper.StoreCategoryMapper;
import com.travel.travelServer.service.BusinessService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 15:38:47
 */
@Service("categoryService")
public class BusinessServiceImpl extends BaseService<BusinessMapper, Business> implements BusinessService {

    @Resource
    private StoreCategoryMapper storeCategoryDao;

    @Resource
    private BusinessMapper categoryDao;

    @Override
    public Page<Business> page(Page<Business> page, Map<String, Object> conditions) {
        LambdaQueryWrapper<Business> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.like(Objects.nonNull(conditions.get("name")) && conditions.get("name") != "", Business::getName, conditions.get("name"));
        return super.page(page,categoryLambdaQueryWrapper);
    }

    @Override
    public Business getById(Long id) {
        return super.getById(id);
    }

    @Override
    public Boolean add(Business pojo) {
        return super.save(pojo);
    }

    @Override
    public Boolean update(Business pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public List<Business> getMyBusiness(Page<Business> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        QueryWrapper<StoreCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StoreCategory::getUserId, userId);
        wrapper.select("DISTINCT category_id");
        List<StoreCategory> storeCategories = storeCategoryDao.selectList(wrapper);
        List<Business> categories = new ArrayList<>();
        storeCategories.stream().forEach(storeCategory -> {
            categories.add(categoryDao.selectById(storeCategory.getCategoryId()));
        });
        return categories;
    }
}

